#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 032

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from euler.numbers.decimal_base import range_for_number_of_digits, \
                                    join_integers, isone_to_nine_pandigital

def get_answer():
    """Question: 
    
    We shall say that an n-digit number is pandigital if it makes use of 
    all the digits 1 to n exactly once; for example, the 5-digit number, 
    15234, is 1 through 5 pandigital.
    
    The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing 
    multiplicand, multiplier, and product is 1 through 9 pandigital.
    
    Find the sum of all products whose multiplicand/multiplier/product identity 
    can be written as a 1 through 9 pandigital.
    
    HINT: Some products can be obtained in more than one way so be sure to 
    only include it once in your sum.
    """

    
    def get_valid_numbers(digits_in_multiplicand, digits_in_multiplier):
        """Returns the valid numbers for all possible combinations of
        all mulitplicands and multipliers for the given integer sizes.
        
        For the purposes of this function, a number is considered valid
        if it is the identity of a multiplicand, a multiplier, and their
        product, as well as a one to nine pandigital number.
        
        Parameters:
            digits_in_multiplicand - The number of digits which the 
                                    multiplicand can have.
                                    
            digits_in_multiplier - The number of digits which the 
                                    multiplier can have.
        """
        
        return [
                multiplicand * multiplier
                    for multiplicand 
                        in range_for_number_of_digits(digits_in_multiplicand)
                    for multiplier
                        in range_for_number_of_digits(digits_in_multiplier)
                    if isone_to_nine_pandigital(
                                                join_integers([
                                                            multiplicand,
                                                            multiplier,
                                                            multiplicand \
                                                                * multiplier
                                                        ])
                                            
                                            )
                ]
    
    #Return result. The multiplicand and multiplier should only have one and 
    #four digits, or one and three digits respectively. This is because
    #1 * 1000 = 1000 and 10 * 100 = 1000, both of which amount to nine digits
    #total.
    return sum(set(get_valid_numbers(1, 4) + get_valid_numbers(2, 3)))

if __name__ == "__main__":
    cProfile.run("print(get_answer())")
